<html>
<head><meta charset="utf-8"><title>implicit crate usage · t-lang · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/index.html">t-lang</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/implicit.20crate.20usage.html">implicit crate usage</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="221723968"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/implicit%20crate%20usage/near/221723968" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> devsnek <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/implicit.20crate.20usage.html#221723968">(Jan 06 2021 at 01:20)</a>:</h4>
<p>so right now if you pass an extern to rustc with a global alloc or a panic handler or #[no_mangle] or #[used] or whatever, it doesn't get actually get linked unless you use something from it or explicitly put <code>extern crate foo</code> in your code. this behaviour seems weird to me, but maybe it is intended? i couldn't find anything explicitly documenting it. if it isn't intended i'd be interested in making it so that the externs are always linked.</p>



<a name="221725861"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/implicit%20crate%20usage/near/221725861" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Eric Huss <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/implicit.20crate.20usage.html#221725861">(Jan 06 2021 at 01:59)</a>:</h4>
<p>I think for the most part is intended. There is a brief oblique mention of the behavior in <a href="https://doc.rust-lang.org/reference/items/extern-crates.html#underscore-imports">https://doc.rust-lang.org/reference/items/extern-crates.html#underscore-imports</a> and <a href="https://doc.rust-lang.org/rustc/lints/listing/allowed-by-default.html#unused-extern-crates">https://doc.rust-lang.org/rustc/lints/listing/allowed-by-default.html#unused-extern-crates</a> (the fact that <code>extern crate foo as _</code> exists solely to force linking implies extern crates are otherwise not linked if unused). Unfortunately linking is a bit of a black art and not well documented.   One example is in Cargo, you can have multiple targets, but not all of them will use every dependency, so those would be sort of wasted work to link.  It was maybe clearer in the past when <code>extern crate</code> was required, but with 2018 edition is maybe less clear.</p>



<a name="221728112"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/implicit%20crate%20usage/near/221728112" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> devsnek <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/implicit.20crate.20usage.html#221728112">(Jan 06 2021 at 02:46)</a>:</h4>
<p>I found the bit about <code>as _</code>, the thing here is that you don't need it. rust thinks they are used if you put extern crate without that, but doesn't link them unless you do.</p>



<a name="221728145"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/implicit%20crate%20usage/near/221728145" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> devsnek <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/implicit.20crate.20usage.html#221728145">(Jan 06 2021 at 02:47)</a>:</h4>
<p>in any case if the idea is that you need to explicitly do something in the source text to make a crate be considered for linking, it is more or less working as intended then.</p>



<a name="221728665"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/implicit%20crate%20usage/near/221728665" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Eric Huss <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/implicit.20crate.20usage.html#221728665">(Jan 06 2021 at 02:59)</a>:</h4>
<p>Yea, <code>as _</code> isn't really necessary since <code>unused_extern_crates</code> lint is allow-by-default.  I think there was some intent to turn it into a warning, but it was reverted due to issues with the lint.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>